﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Abhiruchi_Phase1;
using Abhiruchi_Phase1.BAL_DAL.HRM_DAL;
using System.Data;
using NavigationPaneDemo;

namespace WpfNavigationPane
{
    /// <summary>
    /// Interaction logic for tabUC.xaml
    /// </summary>
    public partial class UC_HRM_PE_CE_MainWindow : UserControl
    {
        public string empMode, eId, fType;
        public UC_HRM_EMPPersonal myPersonal;
        public UC_HRM_EmpOrganizational myOrg;
        public UC_HRM_EmpCommunication myComm;
        RadGridView rgv;
        DataTable myDt = PE_CE_MainWindowDAL.GetEmployeesInfoTable();

        public UC_HRM_PE_CE_MainWindow(string mode)
        {
            InitializeComponent();
            empMode = mode;
            try
            {
                    myPersonal = new UC_HRM_EMPPersonal(mode, "", "NEW");
                    grdPersonal.Children.Add(myPersonal);
                    string eid = myPersonal.eidReturn();
                    myComm = new UC_HRM_EmpCommunication(mode, eid, "NEW");
                    grdComm.Children.Add(myComm);

                    myOrg = new UC_HRM_EmpOrganizational(mode, eid, "New");
                    grdOrg.Children.Add(myOrg);

                    var empList = PE_CE_MainWindowDAL.GetEmployeeMasterEmployeeList();
                    txtFill.ItemsSource = empList;
                
              
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Occured", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        public void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabMain.SelectedIndex == 0)
            {
                lblHeaderPlus.Content = empMode + " - Personal Info";
            }
            else if (tabMain.SelectedIndex == 1)
            {
                lblHeaderPlus.Content = empMode + " - Organizational Info";
            }
            else if (tabMain.SelectedIndex == 2)
            {
                lblHeaderPlus.Content = empMode + " - Communication Info";
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            
            int flag = 0; int count = 0;
           

            for (int i = 0; i < myDt.Rows.Count; i++)
            {
                if ((txtFill.Text.ToUpper() == myDt.Rows[i][0].ToString().ToUpper() || txtFill.Text.ToUpper() == myDt.Rows[i][1].ToString().ToUpper()))
                {
                    eId = myDt.Rows[i][0].ToString();

                    if (myDt.Rows[i][2].ToString() == "Permanent")
                    {
                        empMode = "Permanent Employee";
                        tabMain.SelectedIndex = 0;
                        lblHeaderPlus.Content = empMode + " - Personal Info";
                        BitmapImage src = new BitmapImage();
                        src.BeginInit();
                        src.UriSource = new Uri(getPathofFile() + "//Images//person.ico", UriKind.Relative);
                        src.CacheOption = BitmapCacheOption.OnLoad;

                        src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                        src.EndInit();
                        image1.Source = src;
                    }
                    else if (myDt.Rows[i][2].ToString() == "Contract")
                    {
                        BitmapImage src = new BitmapImage();
                        src.BeginInit();
                        src.UriSource = new Uri(getPathofFile() + "//Images//operator.ico", UriKind.Relative);
                        src.CacheOption = BitmapCacheOption.OnLoad;

                        src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                        src.EndInit();
                        image1.Source = src;
                        empMode = "Contract Employee";
                        tabMain.SelectedIndex = 0;
                        lblHeaderPlus.Content = empMode + " - Personal Info";
                    }
                    //count++;
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                MessageBox.Show("No Employee Found", "Search", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            //if (count>1)
            //{
            //    grdPersonal.Children.Clear();
            //    rgv = new RadGridView();
            //    rgv.ItemsSource = null;
            //    rgv.RowActivated += new EventHandler<Telerik.Windows.Controls.GridView.RowEventArgs>(rgv_RowActivated);
            //    DataTable mydt = PE_CE_MainWindowDAL.GetEmployeesInfoWithNames(txtFill.Text);

            //    if (myDt.Rows.Count > 0)
            //    {
            //        DataRow dtrow;
            //        for (int i = 0; i < 10; i++)
            //        {
            //            dtrow = mydt.NewRow();
            //            dtrow["EId"] = "";
            //            dtrow["EName"] = "";
            //            dtrow["EType"] = "";
            //            mydt.Rows.Add(dtrow);
            //        }
            //        rgv.ItemsSource = mydt;
            //        grdPersonal.Children.Clear();
            //        grdOrg.Children.Clear();
            //        grdComm.Children.Clear();
            //        grdPersonal.Children.Add(rgv);
                   
            //    }
            //    return;
            //}
            if (flag == 1)
            {
                lblType.Content = "Edit";
                grdPersonal.Children.Clear();
                grdOrg.Children.Clear();
                grdComm.Children.Clear();

                myPersonal = new UC_HRM_EMPPersonal(empMode, eId, "EDIT");
                grdPersonal.Children.Add(myPersonal);
                myOrg = new UC_HRM_EmpOrganizational(empMode, eId, "Edit");
                grdOrg.Children.Add(myOrg);
                myComm = new UC_HRM_EmpCommunication(empMode, eId, "EDIT");
                grdComm.Children.Add(myComm);
            }

            var empList = PE_CE_MainWindowDAL.GetEmployeeMasterEmployeeList();
            txtFill.ItemsSource = null;
            txtFill.ItemsSource = empList;
            txtFill.Text = "";


            //myPersonal.initializeContentPersonal(empMode, eId, "EDIT");
            // myOrg.initializeContentOrg(empMode, eId, "Edit");
            //myComm.initializeContentComm(empMode, eId, "EDIT");
        }

        void rgv_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            if (rgv.SelectionMode == SelectionMode.Single)
            {
              //  MessageBox.Show(((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
                eId = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
                empMode = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();

                if (empMode == "Permanent") empMode = "Permanent Employee"; else if (empMode == "Contract") empMode = "Contract Employee";
                lblType.Content = "Edit";
                grdPersonal.Children.Clear();
                grdOrg.Children.Clear();
                grdComm.Children.Clear();

                myPersonal = new UC_HRM_EMPPersonal(empMode, eId, "EDIT");
                grdPersonal.Children.Add(myPersonal);
                myOrg = new UC_HRM_EmpOrganizational(empMode, eId, "Edit");
                grdOrg.Children.Add(myOrg);
                myComm = new UC_HRM_EmpCommunication(empMode, eId, "EDIT");
                grdComm.Children.Add(myComm);
                if (empMode == "Permanent Employee")
                {
                    empMode = "Permanent Employee";
                    tabMain.SelectedIndex = 0;
                    lblHeaderPlus.Content = empMode + " - Personal Info";
                    BitmapImage src = new BitmapImage();
                    src.BeginInit();
                    src.UriSource = new Uri(getPathofFile() + "//Images//person.ico", UriKind.Relative);
                    src.CacheOption = BitmapCacheOption.OnLoad;

                    src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    src.EndInit();
                    image1.Source = src;
                }
                else if (empMode == "Contract Employee")
                {
                    BitmapImage src = new BitmapImage();
                    src.BeginInit();
                    src.UriSource = new Uri(getPathofFile() + "//Images//operator.ico", UriKind.Relative);
                    src.CacheOption = BitmapCacheOption.OnLoad;

                    src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    src.EndInit();
                    image1.Source = src;
                    empMode = "Contract Employee";
                    tabMain.SelectedIndex = 0;
                    lblHeaderPlus.Content = empMode + " - Personal Info";
                }
            }
        }

        //private void radTabControl1_SelectionChanged(object sender, RoutedEventArgs e)
        //{

        //}
        private string getPathofFile()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string[] pathArray = path.Split(new char[] { '\\' });
            path = string.Empty;
            for (int temp = 0; temp < pathArray.Count() - 3; temp++)
            {
                if (temp == 0)
                    path = path + pathArray[temp];
                else
                    path = path + "\\" + pathArray[temp];
            }

            return path;
        }


        private void btnSaveEmp_Click(object sender, RoutedEventArgs e)
        {

            tabMain.SelectedIndex = 0;
            if (!myPersonal.Validation())
            {
                return;
            }

            tabMain.SelectedIndex = 1;
            if (!myOrg.validateForm())
            {
                return;
            }

            tabMain.SelectedIndex = 2;
            if (!myComm.ValidationTesting())
            {
                return;
            }

            if (myPersonal.Validation() == true && myOrg.validateForm() == true && myComm.ValidationTesting() == true)
            {
                tabMain.SelectedIndex = 0;
                myPersonal.btnSave_Click(sender, e);
                myOrg.btnSave_Click(sender, e);
                myComm.btnSave_Click(sender, e);

                grdPersonal.Children.Clear();
                grdOrg.Children.Clear();
                grdComm.Children.Clear();

                myPersonal = new UC_HRM_EMPPersonal(empMode, "", "NEW");
                grdPersonal.Children.Add(myPersonal);
                string eid = myPersonal.eidReturn();
                myComm = new UC_HRM_EmpCommunication(empMode, eid, "NEW");
                grdComm.Children.Add(myComm);

                myOrg = new UC_HRM_EmpOrganizational(empMode, eid, "New");
                grdOrg.Children.Add(myOrg);

                var empList = PE_CE_MainWindowDAL.GetEmployeeMasterEmployeeList();
                txtFill.ItemsSource = null;
                txtFill.ItemsSource = empList;
            }
        }

        private void txtFill_LostFocus(object sender, RoutedEventArgs e)
        {
            //if (txtFill.Text != "")
            //{
            //    btnEdit_Click(null, null);
            //}

            
            //int flag = 0; int count = 0;


            //for (int i = 0; i < myDt.Rows.Count; i++)
            //{
            //    if ((txtFill.Text.ToUpper() == myDt.Rows[i][0].ToString().ToUpper() || txtFill.Text.ToUpper() == myDt.Rows[i][1].ToString().ToUpper()))
            //    {
            //        eId = myDt.Rows[i][0].ToString();

            //        if (myDt.Rows[i][2].ToString() == "Permanent")
            //        {
            //            empMode = "Permanent Employee";
            //            tabMain.SelectedIndex = 0;
            //            lblHeaderPlus.Content = empMode + " - Personal Info";
            //            BitmapImage src = new BitmapImage();
            //            src.BeginInit();
            //            src.UriSource = new Uri(getPathofFile() + "//Images//person.ico", UriKind.Relative);
            //            src.CacheOption = BitmapCacheOption.OnLoad;

            //            src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            //            src.EndInit();
            //            image1.Source = src;
            //        }
            //        else if (myDt.Rows[i][2].ToString() == "Contract")
            //        {
            //            BitmapImage src = new BitmapImage();
            //            src.BeginInit();
            //            src.UriSource = new Uri(getPathofFile() + "//Images//operator.ico", UriKind.Relative);
            //            src.CacheOption = BitmapCacheOption.OnLoad;

            //            src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            //            src.EndInit();
            //            image1.Source = src;
            //            empMode = "Contract Employee";
            //            tabMain.SelectedIndex = 0;
            //            lblHeaderPlus.Content = empMode + " - Personal Info";
            //        }
            //        //count++;
            //        flag = 1;
            //        break;
            //    }
            //}
            //if (flag == 0)
            //{
            // //   MessageBox.Show("No Employee Found", "Search", MessageBoxButton.OK, MessageBoxImage.Warning);
            //}
            
            //if (flag == 1)
            //{
            //    lblType.Content = "Edit";
            //    grdPersonal.Children.Clear();
            //    grdOrg.Children.Clear();
            //    grdComm.Children.Clear();

            //    myPersonal = new UC_HRM_EMPPersonal(empMode, eId, "EDIT");
            //    grdPersonal.Children.Add(myPersonal);
            //    myOrg = new UC_HRM_EmpOrganizational(empMode, eId, "Edit");
            //    grdOrg.Children.Add(myOrg);
            //    myComm = new UC_HRM_EmpCommunication(empMode, eId, "EDIT");
            //    grdComm.Children.Add(myComm);
            //}

            //var empList = PE_CE_MainWindowDAL.GetEmployeeMasterEmployeeList();
            //txtFill.ItemsSource = null;
            //txtFill.ItemsSource = empList;
            ////txtFill.Text = "";


        }

             

       
    }
}
